import configparser
import datetime
import logging
import os.path
from distutils.util import strtobool

DEFAULT_CONFIG_FILE = 'config.ini'
DEFAULT_CONFIG_TYPE = 'UTF-8'


class Settings:
    def __init__(self):
        self.logger = None
        self.input_path = None
        self.output_path = None
        self.current_year = None
        self.db_folder = None
        self.db_file = None
        self.db_path = None
        self.database = None
        self.log_folder = None
        self.log_file = None
        self.log_path = None

        self.base_dir = os.path.dirname(os.path.dirname(__file__))
        self.config = configparser.ConfigParser()
        self.config_path = os.path.join(os.path.dirname(__file__), DEFAULT_CONFIG_FILE)
        self.config.read(self.config_path, DEFAULT_CONFIG_TYPE)

        if self.config.has_option('system', 'input_path'):
            input_dir = self.config['system']['input_path']
            self.input_path = os.path.join(self.base_dir, input_dir)

        if self.config.has_option('system', 'output_path'):
            output_dir = self.config['system']['output_path']
            self.output_path = os.path.join(self.base_dir, output_dir)

        if self.config.has_option('export', 'current_year'):
            self.current_year = self.config['export']['current_year']

        if self.config.has_option('log', 'log_folder'):
            self.log_folder = self.config['log']['log_folder']

        if self.log_folder is not None:
            self.log_path = os.path.join(self.base_dir, self.log_folder)

        if self.config.has_option('log', 'log_file'):
            self.log_file = self.config['log']['log_file']

        if self.config.has_option('database', 'enable'):
            database_enable = strtobool(self.config['database']['enable'])
            if database_enable:
                if self.config.has_option('database', 'db_folder'):
                    self.db_folder = self.config['database']['db_folder']
                    self.db_path = os.path.join(self.base_dir, self.db_folder)
                if self.config.has_option('database', 'db_file'):
                    self.db_file = self.config['database']['db_file']

    def init(self):
        input_path_status = os.path.exists(self.input_path)
        if not input_path_status and len(self.input_path) > 0:
            os.mkdir(self.input_path)

        output_path_status = os.path.exists(self.output_path)
        if not output_path_status and len(self.output_path) > 0:
            os.mkdir(self.output_path)

        if not os.path.exists(self.log_path):
            os.mkdir(self.log_path)

        log_file_path = os.path.join(self.log_path, self.log_file)
        open(log_file_path, 'w').close()
        self.logger = logging.getLogger('system')
        log_format = '[%(asctime)s] %(message)s'
        log_level = logging.ERROR
        log_encoding = 'UTF-8'
        logging.basicConfig(filename=log_file_path, format=log_format, level=log_level, encoding=log_encoding)

        # db_file_path = os.path.join(self.db_path, self.db_file)
        # db_connect = Database.connect(db_file_path)
        # sql = '''create table if not exists leave_rule_table(id integer primary key,year,month,day,status,datetime)'''
        # Database.execute(db_connect=db_connect, sql=sql)

        # db_connect = Database.connect(db_file_path)
        # sql = 'insert into leave_rule_table(year,month,day,status) values (?,?,?,?)'
        # data = (2022, 8, 18, 0)
        # Database.execute(db_connect=db_connect, sql=sql, data=data)

        # db_connect = Database.connect(db_file_path)
        # sql = 'select * from leave_rule_table where year=? and month=? and day=?'
        # data = (2022, 8, 18)
        # result = Database.query(db_connect=db_connect, sql=sql, data=data)
        # print(result)
        pass

    @staticmethod
    def get_datetime(datetime_format='%Y-%m-%d %H:%M:%S'):
        return datetime.datetime.now().strftime(datetime_format)
